<?php

namespace app\index\service;
use app\common\model\oms\CompanyPointRule as CompanyPointRuleModel;
use app\common\model\oms\StaffStore as StaffStoreModel;
use app\common\model\oms\Store as StoreModel;
use app\common\model\log\CompanyPointLog as CompanyPointLogModel;
use app\common\model\oms\CompanyPoint as CompanyPointModel;

class StaffService {
    
    /**
     * 给公司的员工加分
     * @param type $company_id
     * @param type $ruleType
     * @param type $staffId
     * @param type $comment 简单说明
     * @return type
     */
    public function addCompanyStaffPoint($company_id, $ruleType, $staffId, $comment = ''){
        
        try{
            //先获得规则
            $companyPointRuleModel = new CompanyPointRuleModel();
            $companyPointRule = $companyPointRuleModel->getCompanyPointRuleByCondition([
                'o_id'=>$company_id,
                'type'=>$ruleType,
                'is_delete'=>0
            ]);

            if(!$companyPointRule){
                 return true;
                 //throw  new \think\Exception('此集团('.$company_id.')下的积分规则(ruleType:'.$ruleType.')不存在');
            }

            //准备日志
            $logInsert = [
                'o_id' => $company_id, 
                'staff_id' => $staffId, 
                'action' => 1, 
                'point' => $companyPointRule['point'], 
                'status' => 0, 
                'comment' => $comment, 
                'rule_id' => $companyPointRule['id'],
                'store_id' => 0,
                'store_name' => '',
                'add_ts' => time(),
            ];

            //获得当前店员在哪个门店
            $staffStoreModel = new StaffStoreModel();
            $staffStore = $staffStoreModel->getStaffStoreByCondition([
                'staff_id'=>$staffId,
                'is_current'=>1
            ]);

            if($staffStore && !empty($staffStore['store_id'])){ 
                //获得店信息
                $storeModel = new StoreModel();
                $store = $storeModel->getStoreByCondition([
                    'store_id'=>$staffStore['store_id']
                ]);

                $logInsert['store_id'] = $store['store_id'];
                $logInsert['store_name'] = $store['store_show_name'];
            }

            //先创建添加积分的日志记录
            $companyPointLogModel = new CompanyPointLogModel();
            $companyPointLogId = $companyPointLogModel->addCompanyPointLog($logInsert);

            if(!$companyPointLogId){
                throw  new \think\Exception('创建积分变化记录失败');
            }

            //加积分
            $companyPointModel = new CompanyPointModel();
            $companyPoint = $companyPointModel->addCompanyPoint($company_id,$staffId,$companyPointRule['point']);

            if(!$companyPoint){ 
                //加积分失败
                $companyPointLogModel->saveStatus($companyPointLogId, 4); //日志状态改成4
                throw  new \think\Exception('积分添加失败');
            }

            //积分添加成功
            $companyPointLogModel->saveStatus($companyPointLogId, 1); //日志状态改成1

            return true;
            
        } catch (\think\Exception $ex){
            throw new \think\Exception($ex->getMessage());
        }
    }
    
    
}
